//
//  MyWarViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/4/14.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "MyWarViewC.h"
#import "SendWarViewC.h"
#import "Util.h"
#import "UserModel.h"
#import <UIImageView+AFNetworking.h>
#import "WarEntity.h"
#import "WarDetailViewC.h"
#import "AuditPersonViewC.h"
#import <MBProgressHUD.h>

@interface MyWarCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UIImageView *photoImg;
@property(nonatomic, weak) IBOutlet UILabel *personLab;
@property(nonatomic, weak) IBOutlet UILabel *placeLab;
@property(nonatomic, weak) IBOutlet UILabel *eventLab;
@property(nonatomic, weak) IBOutlet UILabel *timeLab;
@property(nonatomic, weak) IBOutlet UILabel *countLab;
@property(nonatomic, weak) IBOutlet UILabel *ncountLab;
@property(nonatomic, weak) IBOutlet UIButton *mywarBtn;
@end
@implementation MyWarCell

@end

@interface MyWarViewC (){
    NSArray *dataArray;
    NSString *num;
    NSString *warid;
    NSString *applyid;
}

@end

@implementation MyWarViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.title =@"我的约战";
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]
                                 initWithImage:
                                 [UIImage imageNamed:@"sendwar"]
                                 style:UIBarButtonItemStylePlain
                                 target:self action:@selector(center)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    [self sendBtnGetData];
    
}

- (void)center{
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    SendWarViewC *sevc = [main instantiateViewControllerWithIdentifier:@"SendWarViewC"];
    [self.navigationController pushViewController:sevc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self sendBtnGetData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)joinBtnGetData{
    NSString *idNum = [NSString stringWithFormat:@"%@",
                       [UserModel sharedInstance].currentUser.ID];
    [[UserModel sharedInstance] warJoinGetWithAccount:idNum warGet:^(id info) {
        NSArray *joinArray = [info copy];
        dataArray = joinArray;
        if (dataArray.count == 0) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.labelText = @"没有数据";
            [HUD hide:YES afterDelay:1.5f];
        }
        num = @"2";
        [self.tableView reloadData];
    }];
}

- (void)sendBtnGetData{
    NSString *idNum = [NSString stringWithFormat:@"%@",
                       [UserModel sharedInstance].currentUser.ID];
    [[UserModel sharedInstance] warSendGetWithAccount:idNum warGet:^(id info) {
        NSArray *sendArray = [info copy];
        dataArray = sendArray;
        if (dataArray.count == 0) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.labelText = @"没有数据";
            [HUD hide:YES afterDelay:1.5f];
        }
        num = @"1";
        [self.tableView reloadData];
    }];
}

- (IBAction)sendBtn:(id)sender{
    [_sendBtn setBackgroundColor:RGBCOLOR(16, 191, 229)];
    [_joinBtn setBackgroundColor:[UIColor grayColor]];
    [_sendBtn setTitleColor:[UIColor whiteColor] forState:
     UIControlStateNormal];
    [_joinBtn setTitleColor:[UIColor whiteColor] forState:
     UIControlStateNormal];
    num = @"1";
    [self sendBtnGetData];
}
- (IBAction)joinBtn:(id)sender{
    [_joinBtn setBackgroundColor:RGBCOLOR(16, 191, 229)];
    [_sendBtn setBackgroundColor:[UIColor grayColor]];
    [_joinBtn setTitleColor:[UIColor whiteColor] forState:
     UIControlStateNormal];
    [_sendBtn setTitleColor:[UIColor whiteColor] forState:
     UIControlStateNormal];
     num = @"2";
    [self joinBtnGetData];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyWarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyWarCellID"
                             forIndexPath:indexPath];
    WarEntity *warEty = dataArray[indexPath.row];
    cell.personLab.text = [NSString stringWithFormat:@"发起人：%@",
                           warEty.accountName];
    cell.placeLab.text = [NSString stringWithFormat:@"场所：%@",
                          warEty.sportsHallName];
    NSString *oritype = [NSString stringWithFormat:@"%@",
                          warEty.sportEventId];
    
    cell.eventLab.text = [NSString stringWithFormat:@"项目：%@",
                          [Util getstrWithsportEventId:oritype]];
    
    cell.timeLab.text = [NSString stringWithFormat:@"时间：%@",
                         warEty.publishTimeStr];
    cell.countLab.text = [NSString stringWithFormat:@"%@",
                          warEty.count];
    cell.ncountLab.text = [NSString stringWithFormat:@"(%@)",
                           warEty.nowcount];
    
    NSString *img = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                     warEty.picPath];
    NSURL *url = [NSURL URLWithString:img];
    [cell.photoImg setImageWithURL:url];
    
    NSString *war = [NSString stringWithFormat:@"%@",
                        warEty.status];
    if ([num isEqualToString:@"1"]) {
        if ([war isEqualToString:@"1"]) {
            [cell.mywarBtn setTitle:@"达成" forState:UIControlStateNormal];
            [cell.mywarBtn setBackgroundColor:RGBCOLOR(250, 141, 69)];
            cell.mywarBtn.enabled = NO;
        }
        if ([war isEqualToString:@"0"]) {
            [cell.mywarBtn setTitle:@"审核" forState:UIControlStateNormal];
            [cell.mywarBtn setBackgroundColor:RGBCOLOR(189, 189, 189)];
            cell.mywarBtn.tag = indexPath.row;
            [cell.mywarBtn addTarget:self action:@selector(auditBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        }

    }
    
    if ([num isEqualToString:@"2"]) {
        
        if ([war isEqualToString:@"1"]) {
            [cell.mywarBtn setTitle:@"达成" forState:UIControlStateNormal];
            [cell.mywarBtn setBackgroundColor:RGBCOLOR(250, 141, 69)];
            cell.mywarBtn.enabled = NO;
        }
        
        if ([war isEqualToString:@"0"]) {
            [cell.mywarBtn setTitle:@"取消" forState:UIControlStateNormal];
            [cell.mywarBtn setBackgroundColor:RGBCOLOR(189, 189, 189)];
            cell.mywarBtn.tag = indexPath.row;
            [cell.mywarBtn addTarget:self action:@selector(auditBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WarEntity *warEty = dataArray[indexPath.row];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    WarDetailViewC *warvc = [main instantiateViewControllerWithIdentifier:@"WarDetailViewC"];
    NSString *idNub = [NSString stringWithFormat:@"%@",
                       warEty.warid];
    warvc.warID = idNub;
    [self.navigationController pushViewController:warvc animated:YES];
    
}

-(void)auditBtnAction:(id)sender{
    
    UIButton *btn=(UIButton *)sender;
    WarEntity *warEty = [dataArray objectAtIndex:btn.tag];
    NSString *count = [NSString stringWithFormat:@"%@",warEty.count];
    NSString *nowcount = [NSString stringWithFormat:@"%@",
                          warEty.nowcount];
    if ([num isEqualToString:@"1"]) {
        if ([count isEqualToString:nowcount] && [nowcount isEqualToString:@"0"]) {
        }
        
        else{
            
            UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                           bundle:nil];
            AuditPersonViewC *audvc = [main instantiateViewControllerWithIdentifier:@"AuditPersonViewC"];
            NSString *idNum = [NSString stringWithFormat:@"%@",warEty.warid];
            audvc.warID = idNum;
            [self.navigationController pushViewController:audvc animated:YES];
        }
    }
    
    else{
        warid = warEty.warid;
        applyid = warEty.yuezhanApplyId;
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                              message:@"确定取消参战吗？"
                                                     delegate:self
                                            cancelButtonTitle:@"取消"
                                        otherButtonTitles:@"确定",nil];
        
        [alert show];
    }

    
    
    
}
//-(void)cannotBtnAction:(id)sender{
//    
//    UIButton *btn=(UIButton *)sender;
//    WarEntity *warEty = [dataArray objectAtIndex:btn.tag];
//    NSString *warid = warEty.warid;
//    NSString *applyid = warEty.yuezhanApplyId;
//    NSString *status =@"2";
//    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
//                                                message:@"请选择一个按钮："
//                                                 delegate:self
//                                        cancelButtonTitle:@"取消"
//                                        otherButtonTitles:@"确定",nil];
//    
//    [alert show];
//    [[UserModel sharedInstance] setApplrWithwarID:warid applyid:applyid status:status getStatus:^(BOOL successed, unsigned int value) {
//        if (successed) {
//            [self.tableView reloadData];
//        }
//        
//    }];

//}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSString* msg = [[NSString alloc] initWithFormat:@"%ld",buttonIndex];
    if ([msg isEqualToString:@"1"]) {
        NSString *status = @"2";
        [[UserModel sharedInstance] setApplrWithwarID:warid applyid:applyid status:status getStatus:^(BOOL successed, unsigned int value) {
            if (successed) {
                [self joinBtnGetData];
            }
        }];
    }
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
